/*!
Response types for an [index exists request](https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-exists.html).
*/

use crate::http::{
    receiver::{
        HttpResponseHead,
        IsOk,
        MaybeOkResponse,
        ParseError,
        ResponseBody,
        Unbuffered,
    },
    StatusCode,
};

/** Response for an [index exists request](https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-exists.html). */
#[derive(Deserialize, Debug)]
pub struct IndicesExistsResponse {
    exists: bool,
}

impl IndicesExistsResponse {
    /** Whether or not the index exists. */
    pub fn exists(&self) -> bool {
        self.exists
    }
}

impl IsOk for IndicesExistsResponse {
    fn is_ok<B: ResponseBody>(
        head: HttpResponseHead,
        body: Unbuffered<B>,
    ) -> Result<MaybeOkResponse<B>, ParseError> {
        match head.status() {
            status if status.is_success() => Ok(MaybeOkResponse::ok(json!({ "exists": true }))),
            StatusCode::NOT_FOUND => Ok(MaybeOkResponse::ok(json!({ "exists": false }))),
            _ => Ok(MaybeOkResponse::err(body)),
        }
    }
}
